package com.zhangh.summer.admin.controller.org;

import com.zhangh.summer.admin.biz.org.OrgBiz;
import com.zhangh.summer.admin.model.bo.DataBO;
import com.zhangh.summer.admin.model.bo.OrgBO;
import com.zhangh.summer.admin.model.bo.OrgQuery;
import com.zhangh.summer.admin.support.base.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-10-08 22:25
 **/
@RestController
@RequestMapping(value = "/org",method = RequestMethod.POST)
public class OrgController {
    @Autowired
    private OrgBiz orgBiz;

    @RequestMapping("/dataList")
    public String dataList(@RequestBody OrgQuery query) {
        List<OrgBO>  orgList=this.orgBiz.getOrgList(query);
        return new JsonResponse().toJson(orgList);
    }

    @RequestMapping("/dataListWithRoot")
    public String dataListWithRoot(@RequestBody OrgQuery query) {
        List<OrgBO> orgList=this.orgBiz.getOrgList(query);
        List<OrgBO> rootList=new ArrayList<>();
        OrgBO orgBO=new OrgBO();
        orgBO.setId(0L);
        orgBO.setLevel(-1);
        orgBO.setOrgName("ROOT");
        orgBO.setChildren(orgList);
        rootList.add(orgBO);
        return new JsonResponse().toJson(rootList);
    }



    @RequestMapping("/getOrg/{id}")
    public String getOrg(@PathVariable Long id) {
        OrgBO orgBO=this.orgBiz.getOrgBO(id);
        return new JsonResponse().toJson(orgBO);
    }

    @RequestMapping("/save")
    public String save(@RequestBody OrgBO orgBO) {
        long c1=this.orgBiz.saveOrg(orgBO);
        return new JsonResponse().toJson(c1);
    }

    @RequestMapping("/update")
    public String update(@RequestBody OrgBO orgBO) {
        int c1=this.orgBiz.updateOrg(orgBO);
        return new JsonResponse().toJson(c1);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody DataBO dataBO) {
        int c1=this.orgBiz.deleteOrg(dataBO.getId());
        return new JsonResponse().toJson(c1);
    }

    @RequestMapping("/updateSort")
    public String updateSort(@RequestBody List<OrgBO> orgBOList) {
        this.orgBiz.orgSort(orgBOList);
        return new JsonResponse().toJson();
    }

}
